package com.amex.uam.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.amex.uam.dao.GeoDao;
import com.amex.uam.model.MS_USER_CTRY_ACSS;
import com.amex.uam.model.MS_USER_RGN_ACSS;
import com.amex.uam.model.Message;
import com.amex.uam.service.GeoService;

@Service("geoService")
public class GeoServiceImpl implements GeoService {

	private GeoDao geoDao;

	@Resource
	public void setGeoDao(GeoDao geoDao) {
		this.geoDao = geoDao;
	}

	@Override
	public Message addUserRegion(String lifExcepRegion, String amexWebId) {
		if (amexWebId == null || amexWebId.equals("")) {
			return new Message(Message.ERROR_PARAM, "amexWebId is invalid");
		}
		if (lifExcepRegion != null) {
			List<MS_USER_RGN_ACSS> list = new ArrayList<MS_USER_RGN_ACSS>();
			String[] items = lifExcepRegion.split(",");
			for (int i = 0; i < items.length; i++) {
				String regionName = items[i].trim();
				if (regionName.length() == 0)
					continue;
				MS_USER_RGN_ACSS region = new MS_USER_RGN_ACSS();
				region.setWORKFORCE_MBR_ID(amexWebId);
				region.setRGN_ABBR_NM(regionName);
				region.setCREAT_TS(new Date());
				list.add(region);
			}
			geoDao.addUserRegion(list);
		}
		return new Message(Message.OK);
	}

	@Override
	public Message addUserCountry(String lifExcepCountry, String amexWebId) {
		if (amexWebId == null || amexWebId.equals("")) {
			return new Message(Message.ERROR_PARAM, "amexWebId is invalid");
		}
		if (lifExcepCountry != null) {
			List<MS_USER_CTRY_ACSS> list = new ArrayList<MS_USER_CTRY_ACSS>();
			String[] items = lifExcepCountry.split(",");
			for (int i = 0; i < items.length; i++) {
				String countryName = items[i].trim();
				if (countryName.length() == 0)
					continue;
				MS_USER_CTRY_ACSS country = new MS_USER_CTRY_ACSS();
				country.setWORKFORCE_MBR_ID(amexWebId);
				country.setCTRY_NM(items[i]);
				country.setCREAT_TS(new Date());
				list.add(country);
			}
			geoDao.addUserCountry(list);
		}
		return new Message(Message.OK);
	}
}
